﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snake
{
    class Node
    {
        public int X { get; set; }
        public int Y { get; set; }
        
        public Node(int posiyionX, int positionY)
        {
            X = posiyionX;
            Y = positionY;
        }
        
        public void MoveUp()
        {
            Y--;
        }

        public void MoveDown()
        {
            Y++;
        }

        public void MoveLeft()
        {
            X--;
        }
        public void MoveRight()
        {
            X++;
        }

        public bool IsOnPosition(int positionX, int positionY)
        {
            if (X != positionX && Y != positionY)
                return true;
            return false;
        }

        public virtual void Move(Func<int, int, Position> position)
        {
            Random rnd = new Random();
            switch (rnd.Next(1, 5))
            {
                case 1:
                    {
                        if (position(X, Y - 1) == Position.Free)
                        //IsOnField(node.X, node.Y - 1) && CrossNodes(node.X, node.Y - 1))
                        {
                            MoveUp();
                        }
                        break;
                    }
                case 2:
                    {
                        if (position(X, Y + 1) == Position.Free)
                        //IsOnField(node.X, node.Y + 1) && CrossNodes(node.X, node.Y + 1))
                        {
                            MoveDown();
                        }
                        break;
                    }
                case 3:
                    {
                        if (position(X - 1, Y) == Position.Free)
                        //IsOnField(node.X - 1, node.Y) && CrossNodes(node.X - 1, node.Y))
                        {
                            MoveLeft();
                        }
                        break;
                    }
                case 4:
                    {
                        if (position(X + 1, Y) == Position.Free)
                        //IsOnField(node.X + 1, node.Y) && CrossNodes(node.X + 1, node.Y))
                        {
                            MoveRight();
                        }
                        break;
                    }

            }
        }
    }

    class ConsoleNode : Node
    {
        public Direction move = Direction.Pause;
        public ConsoleColor ColorNode { get; private set; }
        public char GameNodeSymbol { get; private set; }
        
        public ConsoleNode(int positionX, int positionY, ConsoleColor color, char nodeSymbol)
            : base(positionX, positionY)
        {
            ColorNode = color;
            GameNodeSymbol = nodeSymbol;
        }

        public override void Move(Func<int, int, Position> position)
        {
            if (move == Direction.Left)
            {
                MoveLeft();
            }
            if (move == Direction.Right)
            {
                MoveRight();
            }
            if (move == Direction.Up)
            {
                MoveUp();
            }
            if (move == Direction.Down)
            {
                MoveDown();
            }
        }
        
    }
    enum Direction
    {
        Pause,
        Up,
        Down,
        Left,
        Right
    }
}
